package gui;

import gui.dialogs.DialogSeleccionarCaso;
import gui.dialogs.DialogSeleccionarRed;
import gui.dialogs.DialogSeleccionarSolucion;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import javax.swing.JApplet;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JLabel;

import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.border.TitledBorder;
import javax.swing.UIManager;

import on.Link;
import on.Nodo;
import on.Red;
import opt.Caso;
import opt.Solucion;


import java.awt.Color;
import java.awt.Font;
import java.awt.Dimension;
import java.util.List;
import java.util.TreeSet;

import javax.swing.SwingConstants;
import javax.swing.JList;
import javax.swing.border.EtchedBorder;
import javax.swing.AbstractListModel;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

/*
 * Permisos
grant {
        permission java.security.AllPermission;
};

grant {
   permission java.util.PropertyPermission
      "hibernate.enable_specj_proprietary_syntax", "read";
};

grant {
  permission java.lang.reflect.ReflectPermission
    "suppressAccessChecks";
};

grant {
   permission java.net.SocketPermission "127.0.0.1:5432", "connect";
};
 */

public class Main extends JApplet {
	private static final long serialVersionUID = -647670766790992438L;
	private JTextField textRed;
	private JTextField textCaso;
	private JTextField textSolucion;
	
	private JList nodosList;
	private JList vecinosList;
	private JTextField serviciosPorNodoText;
	private JTextField origenSolicitudesPorNodoText;
	private JTextField destinoSolicitudesPorNodoText;
	
	private JList canalesList;
	private JTextField utilizacionCanal;
	private JList serviciosPorCanalList;
	
	private boolean dialogoAbierto = false;
	
	private List<Red> redes = null;
	private List<Caso> casos = null;
	private List<Solucion> soluciones = null;
	
	private Red red = null;
	private Caso caso = null;
	private Solucion solucion = null;
	
	private EntityManagerFactory emf = Persistence.createEntityManagerFactory("tesis");
	private EntityManager em = emf.createEntityManager();
	
	public Red getRed() {
		return red;
	}

	public void setRed(Red red) {
		this.red = red;
		this.textRed.setText(this.red.toString());
		this.textCaso.setText("");
		this.textSolucion.setText("");
		loadCasos();
	}

	public Caso getCaso() {
		return caso;
	}

	public void setCaso(Caso caso) {
		this.caso = caso;
		this.textCaso.setText(this.caso.toString());
		this.textSolucion.setText("");
		loadSoluciones();
	}

	public Solucion getSolucion() {
		return solucion;
	}

	public void setSolucion(Solucion solucion) {
		this.solucion = solucion;
		this.textSolucion.setText(this.solucion.toString());
		
		nodosList.setModel(new AbstractListModel() {
			private static final long serialVersionUID = -2130216576307786556L;
			private Object [] values = (new TreeSet<Nodo>(red.getNodos())).toArray();
			
			public int getSize() {
				return values.length;
			}
			public Object getElementAt(int index) {
				return values[index];
			}
		});
		
		canalesList.setModel(new AbstractListModel() {
			private static final long serialVersionUID = 4949704630673634080L;
			Object [] values = (new TreeSet<Link>(red.getCanales())).toArray();
			public int getSize() {
				return values.length;
			}
			public Object getElementAt(int index) {
				return values[index];
			}
		});
	}

	public List<Caso> getCasos() {
		return casos;
	}

	public void setCasos(List<Caso> casos) {
		this.casos = casos;
	}

	public List<Solucion> getSoluciones() {
		return soluciones;
	}

	public void setSoluciones(List<Solucion> soluciones) {
		this.soluciones = soluciones;
	}


	private void loadRedes(){
		TypedQuery<Red> q = em.createQuery("select red from Red red", Red.class);
		this.redes = q.getResultList();
	}
	
	private void loadCasos(){
		TypedQuery<Caso> q = em.createQuery("select caso from Caso caso where caso.red = :red", Caso.class);
		q.setParameter("red", this.red);
		this.casos = q.getResultList();
	}
	
	private void loadSoluciones(){
		TypedQuery<Solucion> q = em.createQuery("select sol from Solucion sol where sol.caso = :caso", Solucion.class);
		q.setParameter("caso", this.caso);
		this.soluciones = q.getResultList();
	}
	
	public Main() {
		loadRedes();
		
		getContentPane().setSize(new Dimension(600, 500));
		getContentPane().setPreferredSize(new Dimension(600, 500));
		getContentPane().setMinimumSize(new Dimension(500, 500));
		setSize(new Dimension(600, 500));
		setMinimumSize(new Dimension(600, 500));
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{63, 112, 189, 101, 79, 80, 12, 0};
		gridBagLayout.rowHeights = new int[]{55, 0, 0, 0, 257, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 1.0, 0.0, Double.MIN_VALUE};
		getContentPane().setLayout(gridBagLayout);
		
		JLabel lblNewLabel = new JLabel("Asignacion Optima de Lightpaths");
		lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 18));
		GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
		gbc_lblNewLabel.gridwidth = 3;
		gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel.gridx = 1;
		gbc_lblNewLabel.gridy = 0;
		getContentPane().add(lblNewLabel, gbc_lblNewLabel);
		
		JLabel lblRed = new JLabel("Red");
		GridBagConstraints gbc_lblRed = new GridBagConstraints();
		gbc_lblRed.anchor = GridBagConstraints.EAST;
		gbc_lblRed.insets = new Insets(0, 0, 5, 5);
		gbc_lblRed.gridx = 1;
		gbc_lblRed.gridy = 1;
		getContentPane().add(lblRed, gbc_lblRed);
		
		textRed = new JTextField();
		textRed.setHorizontalAlignment(SwingConstants.CENTER);
		textRed.setBorder(null);
		textRed.setEditable(false);
		textRed.setToolTipText("<no seleccionado>");
		GridBagConstraints gbc_textRed = new GridBagConstraints();
		gbc_textRed.insets = new Insets(0, 0, 5, 5);
		gbc_textRed.fill = GridBagConstraints.HORIZONTAL;
		gbc_textRed.gridx = 2;
		gbc_textRed.gridy = 1;
		getContentPane().add(textRed, gbc_textRed);
		textRed.setColumns(10);
		
		JButton btnSeleccionarRed = new JButton("seleccionar");
		btnSeleccionarRed.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				seleccionarRed();
			}
		});
		GridBagConstraints gbc_btnSeleccionarRed = new GridBagConstraints();
		gbc_btnSeleccionarRed.insets = new Insets(0, 0, 5, 5);
		gbc_btnSeleccionarRed.gridx = 3;
		gbc_btnSeleccionarRed.gridy = 1;
		getContentPane().add(btnSeleccionarRed, gbc_btnSeleccionarRed);
		
		JLabel lblCaso = new JLabel("Caso");
		GridBagConstraints gbc_lblCaso = new GridBagConstraints();
		gbc_lblCaso.anchor = GridBagConstraints.EAST;
		gbc_lblCaso.insets = new Insets(0, 0, 5, 5);
		gbc_lblCaso.gridx = 1;
		gbc_lblCaso.gridy = 2;
		getContentPane().add(lblCaso, gbc_lblCaso);
		
		textCaso = new JTextField();
		textCaso.setHorizontalAlignment(SwingConstants.CENTER);
		textCaso.setBorder(null);
		textCaso.setEditable(false);
		textCaso.setToolTipText("<no seleccionado>");
		textCaso.setColumns(10);
		GridBagConstraints gbc_textCaso = new GridBagConstraints();
		gbc_textCaso.fill = GridBagConstraints.HORIZONTAL;
		gbc_textCaso.insets = new Insets(0, 0, 5, 5);
		gbc_textCaso.gridx = 2;
		gbc_textCaso.gridy = 2;
		getContentPane().add(textCaso, gbc_textCaso);
		
		JButton btnSeleccionarCaso = new JButton("seleccionar");
		btnSeleccionarCaso.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				seleccionarCaso();
			}
		});
		GridBagConstraints gbc_btnSeleccionarCaso = new GridBagConstraints();
		gbc_btnSeleccionarCaso.insets = new Insets(0, 0, 5, 5);
		gbc_btnSeleccionarCaso.gridx = 3;
		gbc_btnSeleccionarCaso.gridy = 2;
		getContentPane().add(btnSeleccionarCaso, gbc_btnSeleccionarCaso);
		
		JButton btnGenerarCaso = new JButton("generar");
		GridBagConstraints gbc_btnGenerarCaso = new GridBagConstraints();
		gbc_btnGenerarCaso.insets = new Insets(0, 0, 5, 5);
		gbc_btnGenerarCaso.gridx = 4;
		gbc_btnGenerarCaso.gridy = 2;
		getContentPane().add(btnGenerarCaso, gbc_btnGenerarCaso);
		
		JLabel lblSolucion = new JLabel("Solucion");
		GridBagConstraints gbc_lblSolucion = new GridBagConstraints();
		gbc_lblSolucion.anchor = GridBagConstraints.EAST;
		gbc_lblSolucion.insets = new Insets(0, 0, 5, 5);
		gbc_lblSolucion.gridx = 1;
		gbc_lblSolucion.gridy = 3;
		getContentPane().add(lblSolucion, gbc_lblSolucion);
		
		textSolucion = new JTextField();
		textSolucion.setHorizontalAlignment(SwingConstants.CENTER);
		textSolucion.setBorder(null);
		textSolucion.setEditable(false);
		textSolucion.setToolTipText("<no seleccionado>");
		textSolucion.setColumns(10);
		GridBagConstraints gbc_textSolucion = new GridBagConstraints();
		gbc_textSolucion.insets = new Insets(0, 0, 5, 5);
		gbc_textSolucion.fill = GridBagConstraints.HORIZONTAL;
		gbc_textSolucion.gridx = 2;
		gbc_textSolucion.gridy = 3;
		getContentPane().add(textSolucion, gbc_textSolucion);
		
		JButton btnSeleccionarSolucion = new JButton("seleccionar");
		btnSeleccionarSolucion.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				seleccionarSolucion();
			}
		});
		GridBagConstraints gbc_btnSeleccionarSolucion = new GridBagConstraints();
		gbc_btnSeleccionarSolucion.insets = new Insets(0, 0, 5, 5);
		gbc_btnSeleccionarSolucion.gridx = 3;
		gbc_btnSeleccionarSolucion.gridy = 3;
		getContentPane().add(btnSeleccionarSolucion, gbc_btnSeleccionarSolucion);
		
		JButton btnGenerarSolucion = new JButton("generar");
		btnGenerarSolucion.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				generarSolucion();
			}
		});
		GridBagConstraints gbc_btnGenerarSolucion = new GridBagConstraints();
		gbc_btnGenerarSolucion.insets = new Insets(0, 0, 5, 5);
		gbc_btnGenerarSolucion.gridx = 4;
		gbc_btnGenerarSolucion.gridy = 3;
		getContentPane().add(btnGenerarSolucion, gbc_btnGenerarSolucion);
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Datos", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.insets = new Insets(0, 0, 5, 5);
		gbc_panel.fill = GridBagConstraints.BOTH;
		gbc_panel.gridwidth = 5;
		gbc_panel.gridx = 1;
		gbc_panel.gridy = 4;
		getContentPane().add(panel, gbc_panel);
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.columnWidths = new int[]{112, 132, 0, 0, 228, 0};
		gbl_panel.rowHeights = new int[]{332, 0, 0};
		gbl_panel.columnWeights = new double[]{1.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		gbl_panel.rowWeights = new double[]{1.0, 1.0, Double.MIN_VALUE};
		panel.setLayout(gbl_panel);
		
		JTabbedPane panelDatos = new JTabbedPane(JTabbedPane.TOP);
		GridBagConstraints gbc_panelDatos = new GridBagConstraints();
		gbc_panelDatos.fill = GridBagConstraints.BOTH;
		gbc_panelDatos.gridwidth = 5;
		gbc_panelDatos.gridheight = 2;
		gbc_panelDatos.insets = new Insets(0, 0, 0, 5);
		gbc_panelDatos.gridx = 0;
		gbc_panelDatos.gridy = 0;
		panel.add(panelDatos, gbc_panelDatos);
		
		JPanel tabNodos = new JPanel();
		panelDatos.addTab("Nodos", null, tabNodos, null);
		GridBagLayout gbl_tabNodos = new GridBagLayout();
		gbl_tabNodos.columnWidths = new int[]{0, 140, 125, 120, 0};
		gbl_tabNodos.rowHeights = new int[]{0, 33, 30, 36, 0, 142, 0, 0};
		gbl_tabNodos.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		gbl_tabNodos.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, Double.MIN_VALUE};
		tabNodos.setLayout(gbl_tabNodos);
		
		JLabel lblNodos = new JLabel("Nodos");
		lblNodos.setFont(new Font("Tahoma", Font.BOLD, 11));
		GridBagConstraints gbc_lblNodos = new GridBagConstraints();
		gbc_lblNodos.insets = new Insets(0, 0, 5, 5);
		gbc_lblNodos.gridx = 1;
		gbc_lblNodos.gridy = 1;
		tabNodos.add(lblNodos, gbc_lblNodos);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.gridheight = 4;
		gbc_scrollPane.insets = new Insets(0, 0, 5, 5);
		gbc_scrollPane.gridx = 1;
		gbc_scrollPane.gridy = 2;
		tabNodos.add(scrollPane, gbc_scrollPane);
		
		nodosList = new JList();
		scrollPane.setViewportView(nodosList);
		nodosList.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent arg0) {
				Nodo nodo = (Nodo) nodosList.getSelectedValue();
				cambiarDescripcionNodo(nodo);
			}
		});
		nodosList.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		
		JLabel lblServicios = new JLabel("Servicios");
		GridBagConstraints gbc_lblServicios = new GridBagConstraints();
		gbc_lblServicios.insets = new Insets(0, 0, 5, 5);
		gbc_lblServicios.gridx = 2;
		gbc_lblServicios.gridy = 1;
		tabNodos.add(lblServicios, gbc_lblServicios);
		
		serviciosPorNodoText = new JTextField();
		serviciosPorNodoText.setEditable(false);
		GridBagConstraints gbc_serviciosPorNodoText = new GridBagConstraints();
		gbc_serviciosPorNodoText.insets = new Insets(0, 0, 5, 0);
		gbc_serviciosPorNodoText.fill = GridBagConstraints.HORIZONTAL;
		gbc_serviciosPorNodoText.gridx = 3;
		gbc_serviciosPorNodoText.gridy = 1;
		tabNodos.add(serviciosPorNodoText, gbc_serviciosPorNodoText);
		serviciosPorNodoText.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel("Origen en Solicitudes");
		GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
		gbc_lblNewLabel_1.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_1.gridx = 2;
		gbc_lblNewLabel_1.gridy = 2;
		tabNodos.add(lblNewLabel_1, gbc_lblNewLabel_1);
		
		origenSolicitudesPorNodoText = new JTextField();
		origenSolicitudesPorNodoText.setEditable(false);
		origenSolicitudesPorNodoText.setColumns(10);
		GridBagConstraints gbc_origenSolicitudesPorNodoText = new GridBagConstraints();
		gbc_origenSolicitudesPorNodoText.insets = new Insets(0, 0, 5, 0);
		gbc_origenSolicitudesPorNodoText.fill = GridBagConstraints.HORIZONTAL;
		gbc_origenSolicitudesPorNodoText.gridx = 3;
		gbc_origenSolicitudesPorNodoText.gridy = 2;
		tabNodos.add(origenSolicitudesPorNodoText, gbc_origenSolicitudesPorNodoText);
		
		JLabel lblDestinoEnSolicitudes = new JLabel("Destino en Solicitudes");
		GridBagConstraints gbc_lblDestinoEnSolicitudes = new GridBagConstraints();
		gbc_lblDestinoEnSolicitudes.insets = new Insets(0, 0, 5, 5);
		gbc_lblDestinoEnSolicitudes.gridx = 2;
		gbc_lblDestinoEnSolicitudes.gridy = 3;
		tabNodos.add(lblDestinoEnSolicitudes, gbc_lblDestinoEnSolicitudes);
		
		destinoSolicitudesPorNodoText = new JTextField();
		destinoSolicitudesPorNodoText.setEditable(false);
		destinoSolicitudesPorNodoText.setColumns(10);
		GridBagConstraints gbc_destinoSolicitudesPorNodoText = new GridBagConstraints();
		gbc_destinoSolicitudesPorNodoText.insets = new Insets(0, 0, 5, 0);
		gbc_destinoSolicitudesPorNodoText.fill = GridBagConstraints.HORIZONTAL;
		gbc_destinoSolicitudesPorNodoText.gridx = 3;
		gbc_destinoSolicitudesPorNodoText.gridy = 3;
		tabNodos.add(destinoSolicitudesPorNodoText, gbc_destinoSolicitudesPorNodoText);
		
		JLabel lblVecinos = new JLabel("Vecinos");
		lblVecinos.setFont(new Font("Tahoma", Font.BOLD, 11));
		GridBagConstraints gbc_lblVecinos = new GridBagConstraints();
		gbc_lblVecinos.insets = new Insets(0, 0, 5, 5);
		gbc_lblVecinos.gridx = 2;
		gbc_lblVecinos.gridy = 4;
		tabNodos.add(lblVecinos, gbc_lblVecinos);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		GridBagConstraints gbc_scrollPane_1 = new GridBagConstraints();
		gbc_scrollPane_1.fill = GridBagConstraints.BOTH;
		gbc_scrollPane_1.insets = new Insets(0, 0, 5, 5);
		gbc_scrollPane_1.gridx = 2;
		gbc_scrollPane_1.gridy = 5;
		tabNodos.add(scrollPane_1, gbc_scrollPane_1);
		
		vecinosList = new JList();
		scrollPane_1.setViewportView(vecinosList);
		
		JPanel tabCanales = new JPanel();
		panelDatos.addTab("Canales", null, tabCanales, null);
		GridBagLayout gbl_tabCanales = new GridBagLayout();
		gbl_tabCanales.columnWidths = new int[]{0, 0, 81, 0, 88, 86, 0, 0, 0};
		gbl_tabCanales.rowHeights = new int[]{0, 0, 42, 0, 47, 49, 0, 0};
		gbl_tabCanales.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, Double.MIN_VALUE};
		gbl_tabCanales.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, Double.MIN_VALUE};
		tabCanales.setLayout(gbl_tabCanales);
		
		JLabel lblCanales = new JLabel("Canales");
		lblCanales.setFont(new Font("Tahoma", Font.BOLD, 11));
		GridBagConstraints gbc_lblCanales = new GridBagConstraints();
		gbc_lblCanales.insets = new Insets(0, 0, 5, 5);
		gbc_lblCanales.gridx = 2;
		gbc_lblCanales.gridy = 1;
		tabCanales.add(lblCanales, gbc_lblCanales);
		
		JScrollPane scrollPane_2 = new JScrollPane();
		scrollPane_2.setPreferredSize(new Dimension(50, 300));
		scrollPane_2.setSize(new Dimension(50, 300));
		scrollPane_2.setMaximumSize(new Dimension(100, 32767));
		GridBagConstraints gbc_scrollPane_2 = new GridBagConstraints();
		gbc_scrollPane_2.gridheight = 4;
		gbc_scrollPane_2.insets = new Insets(0, 0, 5, 5);
		gbc_scrollPane_2.fill = GridBagConstraints.BOTH;
		gbc_scrollPane_2.gridx = 2;
		gbc_scrollPane_2.gridy = 2;
		tabCanales.add(scrollPane_2, gbc_scrollPane_2);
		
		canalesList = new JList();
		scrollPane_2.setViewportView(canalesList);
		canalesList.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent arg0) {
				Link canal = (Link) canalesList.getSelectedValue();
				cambiarDescripcionCanal(canal);
			}
		});
		
		JLabel lblUtilizacion = new JLabel("Utilizacion");
		GridBagConstraints gbc_lblUtilizacion = new GridBagConstraints();
		gbc_lblUtilizacion.anchor = GridBagConstraints.EAST;
		gbc_lblUtilizacion.insets = new Insets(0, 0, 5, 5);
		gbc_lblUtilizacion.gridx = 4;
		gbc_lblUtilizacion.gridy = 2;
		tabCanales.add(lblUtilizacion, gbc_lblUtilizacion);
		
		utilizacionCanal = new JTextField();
		utilizacionCanal.setEditable(false);
		GridBagConstraints gbc_utilizacionCanal = new GridBagConstraints();
		gbc_utilizacionCanal.insets = new Insets(0, 0, 5, 5);
		gbc_utilizacionCanal.fill = GridBagConstraints.HORIZONTAL;
		gbc_utilizacionCanal.gridx = 5;
		gbc_utilizacionCanal.gridy = 2;
		tabCanales.add(utilizacionCanal, gbc_utilizacionCanal);
		utilizacionCanal.setColumns(10);
		
		JLabel lblServicios_1 = new JLabel("Servicios");
		lblServicios_1.setFont(new Font("Tahoma", Font.BOLD, 11));
		GridBagConstraints gbc_lblServicios_1 = new GridBagConstraints();
		gbc_lblServicios_1.insets = new Insets(0, 0, 5, 5);
		gbc_lblServicios_1.gridx = 4;
		gbc_lblServicios_1.gridy = 3;
		tabCanales.add(lblServicios_1, gbc_lblServicios_1);
		
		JScrollPane scrollPane_3 = new JScrollPane();
		GridBagConstraints gbc_scrollPane_3 = new GridBagConstraints();
		gbc_scrollPane_3.gridheight = 2;
		gbc_scrollPane_3.insets = new Insets(0, 0, 5, 5);
		gbc_scrollPane_3.fill = GridBagConstraints.BOTH;
		gbc_scrollPane_3.gridx = 4;
		gbc_scrollPane_3.gridy = 4;
		tabCanales.add(scrollPane_3, gbc_scrollPane_3);
		
		serviciosPorCanalList = new JList();
		scrollPane_3.setViewportView(serviciosPorCanalList);
		
		JPanel tabServicios = new JPanel();
		panelDatos.addTab("Servicios", null, tabServicios, null);
		
		JPanel tabSolicitudes = new JPanel();
		panelDatos.addTab("Solicitudes", null, tabSolicitudes, null);
	}
	
	private void seleccionarRed(){
		if(dialogoAbierto) return;
		
		dialogoAbierto = true;
		
		DialogSeleccionarRed dialogRed = new DialogSeleccionarRed();
		dialogRed.setRedes(redes);
		dialogRed.setVisible(true);
		dialogRed.setBounds(200, 100, 500, 300);
		dialogRed.setParent(this);
	}
	
	private void seleccionarCaso(){
		if(dialogoAbierto || red == null) return;
		
		dialogoAbierto = true;
		
		DialogSeleccionarCaso dialogCaso = new DialogSeleccionarCaso();
		dialogCaso.setCasos(casos);
		dialogCaso.setVisible(true);
		dialogCaso.setBounds(200, 100, 500, 300);
		dialogCaso.setParent(this);
	}
	
	private void seleccionarSolucion(){
		if(dialogoAbierto || caso == null) return;
		
		dialogoAbierto = true;
	
		DialogSeleccionarSolucion dialogSolucion = new DialogSeleccionarSolucion();
		dialogSolucion.setSoluciones(soluciones);
		dialogSolucion.setVisible(true);
		dialogSolucion.setBounds(200, 100, 500, 300);
		dialogSolucion.setParent(this);
	}
	
	private void generarSolucion(){
		Solucion solucion = new Solucion();
		solucion.setCaso(this.caso);
		solucion.random();
		
		solucion.setNombre("SolGenerada");
		
		em.getTransaction().begin();
		em.persist(solucion);
		em.getTransaction().commit();
		
		JOptionPane.showMessageDialog(this, "Se genero una nueva solucion : " + solucion);
		
		soluciones.add(solucion);
	}
	
	public void cerrarDialogo(){
		dialogoAbierto = false;
	}
	
	private void cambiarDescripcionNodo(Nodo nodo){		
		String sQuery = "";
		sQuery += "select servicio";
		sQuery += "  from Servicio servicio";
		sQuery += "  join servicio.primario.saltos as saltoP";
		sQuery += " where (saltoP.enlace.canal.extremoA = :nodo or saltoP.enlace.canal.extremoB = :nodo )";
		sQuery += " union ";
		sQuery += "select servicio";
		sQuery += "  from Servicio servicio";
		sQuery += "  join servicio.alternativo.saltos as saltoA";
		sQuery += " where (saltoA.enlace.canal.extremoA = :nodo or saltoA.enlace.canal.extremoB = :nodo )";
		Query q = em.createQuery(sQuery);
		q.setParameter("nodo", nodo);
		serviciosPorNodoText.setText("" + q.getResultList().size());
		
		sQuery  = "select solicitud";
		sQuery += "  from Solicitud solicitud";
		sQuery += " where solicitud.origen = :nodo";
		q = em.createQuery(sQuery);
		q.setParameter("nodo", nodo);
		origenSolicitudesPorNodoText.setText("" + q.getResultList().size());
		
		sQuery  = "select solicitud";
		sQuery += "  from Solicitud solicitud";
		sQuery += " where solicitud.destino = :nodo";
		q = em.createQuery(sQuery);
		q.setParameter("nodo", nodo);
		destinoSolicitudesPorNodoText.setText("" + q.getResultList().size());
		
		DefaultListModel alm = new DefaultListModel();
		for(Link canal: nodo.getLinks()){
			Nodo vecino = canal.getDestination();
			alm.addElement(vecino);
		}
		
		vecinosList.setModel(alm);
	}
	
	private void cambiarDescripcionCanal(Link canal){
		String sQuery = "";
		sQuery += "select concat(servicio.solicitud.origen,'-', servicio.solicitud.destino, ' primario')";
		sQuery += "  from Solucion solucion";
		sQuery += "  join solucion.servicios as servicio";
		sQuery += "  join servicio.primario.saltos as saltoP";
		sQuery += " where saltoP.enlace.canal = :canal";
		sQuery += "   and solucion = :solucion";
		sQuery += " union ";
		sQuery += "select concat(servicio.solicitud.origen,'-', servicio.solicitud.destino, ' alternativo')";
		sQuery += "  from Solucion solucion";
		sQuery += "  join solucion.servicios as servicio";
		sQuery += "  join servicio.alternativo.saltos as saltoA";
		sQuery += " where saltoA.enlace.canal = :canal";
		sQuery += "   and servicio.solicitud.nivel = Nivel.Oro";
		sQuery += "   and solucion = :solucion";
		TypedQuery<String> q = em.createQuery(sQuery,String.class);
		q.setParameter("canal", canal);
		q.setParameter("solucion", solucion);
		
		List<String> servicios = q.getResultList();
		DefaultListModel dlm = new DefaultListModel();
		for(String s: servicios){
			dlm.addElement(s);
		}
		serviciosPorCanalList.setModel(dlm);
		
		int utilizacion =(int) (100.0*servicios.size()/canal.getEnlaces().size());
		utilizacionCanal.setText( utilizacion + "%" );
	}

	
	public void inicializarDatos(){
		nodosList.setModel(new AbstractListModel() {
			private static final long serialVersionUID = -4301252989479152358L;
			String[] values = new String[1];
			public int getSize() {
				return values.length;
			}
			public Object getElementAt(int index) {
				return values[index];
			}
		});
		
		canalesList.setModel(new AbstractListModel() {
			private static final long serialVersionUID = 2450807442314496808L;
			String[] values = new String[1];
			public int getSize() {
				return values.length;
			}
			public Object getElementAt(int index) {
				return values[index];
			}
		});
	}
	
	
}
